﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AvalonDock;
using RiskMan.DataClass.Models;
using RiskMan.Config;

namespace RiskMan.Views.LimitStock
{
    public partial class LimitStockView : DocumentContent
    {


        public void LogicMethod(Object sender, EventArgs args)
        {
            bool running = false;

            if (mainWindow.Terminals == null)
                running = false;
            else
            {
                if (mainWindow.Terminals.CountConnected() > 0)
                    running = true;
                else
                    running = false;
            }

            buttonStart.IsEnabled = running;
            button4.IsEnabled = running;

            if (running == true)
            {
                foreach (var item in GetCount(mainWindow._dataProvider.EmitentLimiteList))
                {
                    bool addIntsument = false;
                    foreach (var item2 in positionModel)
                    {
                        if (item.Seccode == item2.Seccode && item.ClientCode == item2.ClientCode)
                        {
                            addIntsument = true;
                            if (item.Count != item2.Count)
                            {
                                item2.Message = false;
                                item2.Count = item.Count;


                            }
                        }
                    }
                    if (addIntsument == false)
                        positionModel.Add(item);
                }

                foreach (var item in GetCount(mainWindow._dataProvider.FortsPositionsList))
                {
                    bool addIntsument = false;
                    foreach (var item2 in positionModel)
                    {
                        if (item.Seccode == item2.Seccode && item.ClientCode == item2.ClientCode)
                        {
                            addIntsument = true;
                            if (item.Count != item2.Count)
                            {
                                item2.Message = false;
                                item2.Count = item.Count;
                            }
                        }
                    }
                    if (addIntsument == false)
                        positionModel.Add(item);
                }


                List<string> listRunningClietCode = RunningClientCode();

                for (int i = 0; i < positionModel.Count; i++)
                {
                    _formate = cfg.SetFormate(mainWindow._dataProvider.GetAccuraccy(positionModel[i].Seccode));

                    WorkingTimeClass time = new WorkingTimeClass(positionModel[i].Account);
                    if (time.IsWork() == true)
                    {
                        if (IsRun(positionModel[i]) == true)
                        {
                            if (positionModel[i].Count != 0)
                            {
                                new LogicLimit(positionModel[i], mainWindow, _formate).Start();
                                if (RiskMan.Views.LimitStock.LogicLimit.logLimitStock != null)
                                    lsbxLimitStock.Items.Add(RiskMan.Views.LimitStock.LogicLimit.logLimitStock);
                            }
                        }
                        //Учитывает входит ли инструмент в список разрешенных
                        if (listRunningClietCode.Count > 0)
                            for (int j = 0; j < listRunningClietCode.Count; j++)
                            {
                                if (listRunningClietCode[j].ToString() == positionModel[i].ClientCode && IsRun(positionModel[i]) == false && positionModel[i].Count != 0)
                                {
                                    new LogicLimit(positionModel[i], mainWindow, _formate).Inspection();
                                    if (RiskMan.Views.LimitStock.LogicLimit.logLimitStock != null)
                                        lsbxLimitStock.Items.Add(RiskMan.Views.LimitStock.LogicLimit.logLimitStock);
                                }
                            }
                    }
                }
            }
        }
    }
}
